package org.example.gradle.jp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import gradle.common.core.response.ServerResponse;
import gradle.mybatis.common.response.ApiResponse;
import gradle.mybatis.common.utils.PageUtil;
//import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.gradle.jp.feigns.GradleAdminFeignClient;
import org.example.gradle.jp.mapper.StarWordMapper;
import org.example.gradle.jp.model.dto.StarWordAddDto;
import org.example.gradle.jp.model.dto.StarWordQueryDto;
import org.example.gradle.jp.model.entity.StarWord;
import org.example.gradle.jp.model.entity.StarWord2;
import org.example.gradle.jp.model.entity.StarWordManyEntity;
import org.example.gradle.jp.model.vo.StarWordVo;
import org.example.gradle.jp.service.StarWordService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class StarWordImpl implements StarWordService {

    @Resource
    private StarWordMapper starWordMapper;
    @Resource
    private GradleAdminFeignClient gradleAdminFeignClient;

    @Override
    public void insert(StarWordAddDto starWordDto) {

        StarWord starWord = BeanUtil.copyProperties(starWordDto, StarWord.class);

        starWord.setCreateTime(LocalDateTime.now());
        starWord.setUpdateUser(0L);
        starWord.setUpdateTime(LocalDateTime.now());


        starWordMapper.insert(starWord);
    }

    @Override
    public void insert2(String a, Integer b, StarWordAddDto starWordDto) {
        StarWord starWord = BeanUtil.copyProperties(starWordDto, StarWord.class);

        starWord.setCreateTime(LocalDateTime.now());
        starWord.setUpdateUser(0L);
        starWord.setUpdateTime(LocalDateTime.now());


        starWordMapper.insert2(a, b, starWord);
    }

    @Override
    public void starWordManyEntityInsertTest(StarWordManyEntity starWordDto) {
        starWordMapper.starWordManyEntityInsertTest(starWordDto);
    }

    @Override
    public void batchInsert(List<StarWordAddDto> starWordDto) {

        List<StarWord> starWord = BeanUtil.copyToList(starWordDto, StarWord.class);

        starWordMapper.batchInsert(starWord);
    }

    @Override
    public void batchInsert2(List<StarWordAddDto> starWordDto) {

        List<StarWord2> StarWord2 = BeanUtil.copyToList(starWordDto, StarWord2.class);

        starWordMapper.batchInsert2(StarWord2);
    }

    @Override
    public ApiResponse<List<StarWordVo>> listPage(StarWordQueryDto starWordQueryDto) {
        Page<List<StarWordVo>> page = PageUtil.startPage(starWordQueryDto.getPageNum(), starWordQueryDto.getPageSize());

        List<StarWordVo> listPage = starWordMapper.listPage(starWordQueryDto);

        return ApiResponse.success(listPage, page);
    }

    //seata 分布式事务测试
//    @GlobalTransactional
    @Override
    public Boolean seataTransactionalTest() {
        StarWord starWord = new StarWord();
        starWord.setStarCourseId(0L);
        starWord.setWordSampleId(0L);
        starWord.setWordOriginal("1");
        starWord.setWordChinese("1");
        starWord.setWordType("1");
        log.info("starWordMapper  ->  insert()");
        starWordMapper.insert(starWord);


        log.info("gradleAdminFeignClient  ->  addUser()");
        ServerResponse<Boolean> booleanServerResponse = gradleAdminFeignClient.addUser();

        if (Objects.isNull(booleanServerResponse) || booleanServerResponse.getStatus() != 200) {
            return false;
        }

        return true;
    }
}
